// pages/Find/find/find.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    s: app.system.statusBarHeight, //状态栏高度
    n: (app.menu.top - app.system.statusBarHeight) * 2 + app.menu.height, //导航栏高度
    h: app.menu.height, //胶囊高度
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,//阿里云公共地址
    tabs: ['找展会', '新资讯'],
    activeIndex: 0,//选择的下标
    serveShow: false,
    page: 1,//页码
    pagingState: false,//分页状态
    ExhibitionList: [],//商品
    articlelist: [],//新资讯
    banner: {},//广告位置
    keywords: '',//搜索内容
    userinfo: getApp().userinfo,
  },
  /**图片预览 */
  previewImage(event) {
    wx.previewImage({
      current: this.data.serveInfo.wechat_image, // 当前显示图片的http链接
      urls: [this.data.serveInfo.wechat_image]
    });
  },
  /**获取客服配置 */
  async getConfig() {
    const { code, data } = await getApp().api.getConfig()
    if (code == 1) {
      this.setData({
        serveInfo: data
      })
    }
  },
  /**搜索 */
  search() {
    let keywords = this.data.keywords
    if (!keywords) return getApp().showToast('请输入您要搜索的内容！')
    if (this.data.activeIndex == 0) {
      this.setData({
        page: 1,
        pagingState: false,
        ExhibitionList: [],
      })
      this.getExhibitionList(keywords)
    } else {
      this.setData({
        page: 1,
        pagingState: false,
        articlelist: [],
      })
      this.articlelists(keywords)
    }

  },
  /**清空 */
  clearKeyword() {
    console.log(1);
    this.setData({
      keywords: '',
      page: 1,//页码
      pagingState: false,//分页状态
      articlelist: [],//商圈列表
      ExhibitionList: [],
    })
    let keywords = this.data.keywords
    this.articlelists(keywords)
    this.getExhibitionList(keywords)
  },
  /**双休数据绑定 */
  model(event) {
    this.setData({
      keywords: event.detail.value
    })
  },
  /**下拉刷新 */
  refresherrefresh(e) {
    // console.log(e);
    let index = this.data.activeIndex
    if (index == 0) {
      this.setData({
        page: 1,//页码
        pagingState: false,//分页状态
        ExhibitionList: [],//商圈列表
      })
      this.getExhibitionList()
    } else {
      this.setData({
        page: 1,//页码
        pagingState: false,//分页状态
        articlelist: [],//商圈列表
      })
      this.articlelists()
    }
  },
  /** */
  async articlelists(keywords) {
    let object = {
      page_no: this.data.page,
      name: keywords || ''
    }
    // const { code, data } = await getApp().api.()
    if (!this.data.pagingState) {
      const { code, data } = await getApp().api.articlelists(object)
      if (code == 1) {
        this.setData({
          isenabled: false,
          articlelist: this.data.articlelist.concat(data.list)
        })
        if (data.more != 1) {
          this.setData({
            pagingState: true
          })
          return
        }
        this.setData({
          page: (this.data.page * 1) + 1
        })
      }
    }
  },
  /**找展会 */
  async getExhibitionList(keywords) {
    let object = {
      page_no: this.data.page,
      name: keywords || ''
    }
    if (!this.data.pagingState) {
      const { code, data } = await getApp().api.getExhibitionList(object)
      if (code == 1) {
        this.setData({
          isenabled: false,
          ExhibitionList: this.data.ExhibitionList.concat(data.list).map(item => {
            item.end_time = getApp().timestampToDate(item.end_time * 1000, 'Y-m-d')
            return item
          })
        })
        if (data.more != 1) {
          this.setData({
            pagingState: true
          })
          return
        }
        this.setData({
          page: (this.data.page * 1) + 1
        })
      }
    }
  },
  /**广告位 */
  async getPositionList() {
    let object = {
      ad_sn: 'exhibitionBanner'
    }
    const { code, data } = await getApp().api.getPositionList(object)
    if (code == 1) {
      this.setData({
        banner: data
      })
    }
  },
  onOpen() {
    this.setData({ serveShow: true });
  },
  onClose() {
    this.setData({ serveShow: false });
  },
  navigateTo(event) {
    console.log(event);
    let { url, item, type, details } = event.currentTarget.dataset
    let TTUL = this
    wx.navigateTo({
      url,
      success: function (res) {
        if (TTUL.data.activeIndex == 0) {
          if (details != 'Details') {
            res.eventChannel.emit('exhibitionInfo', { type, ...item })
          }
        }
      }
    })
  },
  /**
   * 滑动切换
   * @param {} e 
   */
  changeItem(e) {
    console.log(e);
    const index = e.detail.current;
    this.setData({
      activeIndex: index,
    });
  },
  /**
   * tabs切换
   * @param {*} e 
   */
  onTabTap(e) {
    console.log(getApp());
    const index = e.currentTarget.dataset.index;
    this.setData({
      activeIndex: index,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getExhibitionList()
    this.getPositionList()
    this.articlelists()
    this.getConfig()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})